package cn.com.tomato.controller;

import cn.com.tomato.controller.domain.AjaxResult;
import cn.com.tomato.controller.domain.todoSet.SortVo;
import cn.com.tomato.controller.domain.todoSet.TodoSetVo;
import cn.com.tomato.model.TodoSet;
import cn.com.tomato.service.TodoSetService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 待办集合
 */
@RestController
@RequestMapping("/tomato/todoSet")
public class TodoSetController extends BaseController
{

    @Autowired
    private TodoSetService todoSetService;

    @PostMapping("/getAll")
    public Object getAll(@RequestBody() JSONObject json){
        TodoSet param = new TodoSet();
        param.setFinish(json.getIntValue("isFinish"));
        List<TodoSet> todoSets = todoSetService.selectTodoSetList(param);
        return todoSets;
    }

    /**
     * 新增待办集
     * @param todoSet
     * @return
     */
    @PostMapping()
    public AjaxResult add(@RequestBody TodoSetVo todoSet){
        if(todoSet.getId() != null) {
            return error("新增id必须为空");
        }
        TodoSet result = todoSet.parse();
        int count = todoSetService.insertTodoSet(result);
        return success(result);
    }

    /**
     * 更新待办集
     */
    @PutMapping()
    public AjaxResult update(@RequestBody TodoSetVo todoSet){
        System.out.println("todoSet = " + todoSet);
        if(todoSet.getId() == null) {
            return error("更新id必须不为空");
        }
        int count = todoSetService.updateTTodoSet(todoSet.parse());
        return success();
    }

    /**
     * 删除待办集
     */
    @DeleteMapping ("/{id}")
    public AjaxResult delete(@PathVariable(name = "id") Long id){
        todoSetService.deleteTodoSetById(id);
        return success();
    }

    /**
     * 更新待办集排序
     */
    @PostMapping ("/sortUpdate")
    public AjaxResult sortUpdate(@RequestBody List<SortVo> sortUpdates){
        todoSetService.updateSort(sortUpdates);
        return success();
    }

}